﻿/*----------------------------------------------------------------
           // Copyright (C) 2008 安华顺通(北京)科技有限公司
           // 版权所有。 
           //
           // 文件名：ObjectList.cs
           // 文件功能描述：提供同类型的对象列表。
           //
           // 
           // 创建标识：
           //
           // 修改标识：
           // 修改描述：
           //
           // 修改标识：
           // 修改描述：
//----------------------------------------------------------------*/

#region UsingStatements

using System.Collections.Generic;
using System.Xml;

#endregion

namespace RuleCode.ObjectTree
{
    internal class ObjectList
    {
        private static ObjectList instance;
        private bool tableListExpire = true;
        private List<string> tableList;
        private bool tableFieldListExpire = true;
        private List<string> tableFieldList;
        private bool tableGroupFieldExpire = true;
        private List<string> tableGroupField;
        private bool tableRelationFieldExpire = true;
        private List<string> tableRelationField;
        private bool tableRelationRelatedFieldExpire = true;
        private List<string> tableRelationRelatedField;
        private bool queryDataSourceTableFieldsExpire = true;
        private List<string> queryDataSourceTableFields;
        private List<string> tableGroupList;
        private bool tableGroupListExpire = true;
        private bool enumsListExpire = true;
        private List<string> enumsList;
        private bool webFormFieldGroupListExire = true;
        private List<string> webFormFieldGroupList;
        private readonly string TablePath = "对象设计器/数据字典/表/";
        private readonly string FieldsPath = "/Fields/*";

        /// <summary>
        /// 单态的实例化对象
        /// </summary>
        public static ObjectList Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new ObjectList();
                }
                return instance;
            }
        }

        /// <summary>
        /// 按照类型设置过期标致
        /// </summary>
        /// <param name="type"></param>
        public void SetTypeExpire(string type)
        {
            switch (type)
            {
                case OT.Table:
                    {
                        tableListExpire = true;
                        break;
                    }
            }
        }

        public bool GetTypeExpire(string type)
        {
            bool ret = false;
            ;
            switch (type)
            {
                case OT.Table:
                    {
                        ret = tableFieldListExpire;
                        break;
                    }
                case OT.TableRelationRelatedField:
                    {
                        ret = tableRelationRelatedFieldExpire;
                        break;
                    }
                case OT.TableFieldGroupField:
                    {
                        ret = tableGroupFieldExpire;
                        break;
                    }
                case OT.TableRelationField:
                    {
                        ret = tableRelationFieldExpire;
                        break;
                    }
                case OT.TableFieldGroups:
                    {
                        ret = tableGroupListExpire;
                        break;
                    }
                case OT.BaseEnums:
                    {
                        ret = enumsListExpire;
                        break;
                    }
                default:
                    {
                        ret = false;
                        break;
                    }
            }
            return ret;
        }

        /// <summary>
        /// TableList
        /// </summary>
        public List<string> TableList
        {
            get
            {
                if (tableList == null || tableListExpire)
                {
                    tableList = new List<string>();
                    tableList.Clear();
                    XmlNodeList nodeList = Util.SysXmlDocument.SelectNodes(TablePath + "*");
                    foreach (XmlNode node in nodeList)
                    {
                        tableList.Add(node.Name);
                    }
                }
                return tableList;
            }
        }

        public List<string> TableFieldList
        {
            get
            {
                if (tableFieldList == null || tableFieldListExpire)
                {
                    tableFieldList = new List<string>();
                    tableFieldList.Clear();
                    XmlNodeList nodelist =
                        Util.SysXmlDocument.SelectNodes(TablePath +
                                                        ((EDTRelationProperty)Util.PropertyGrid.SelectedObject).Table +
                                                        "/Fields/*");
                    tableFieldList.Add("RECID");
                    foreach (XmlNode node in nodelist)
                    {
                        tableFieldList.Add(node.Name);
                    }
                }
                return tableFieldList;
            }
        }
        public List<string> TableGroupList
        {
            get
            {
                if (tableGroupList == null || tableGroupListExpire)
                {
                    tableGroupList = new List<string>();
                    tableGroupList.Clear();
                    string tablename = ((GroupProperty)Util.PropertyGrid.SelectedObject).DataSource;
                    //如果设置了Table则检索Table下的Group，否则返回空列表
                    if (!string.IsNullOrEmpty(tablename))
                    {
                        XmlNodeList nodeList = Util.SysXmlDocument.SelectNodes(TablePath + tablename + "/FieldGroups/*");
                        foreach (XmlNode node in nodeList)
                        {
                            tableGroupList.Add(node.Name);
                        }
                    }
                }
                return tableGroupList;
            }
        }

        //todo:此处的TableName不好取，暂不实现下拉功能
        public List<string> WebFormFieldGroupList
        {
            get
            {
                if (webFormFieldGroupList == null || webFormFieldGroupListExire)
                {
                    webFormFieldGroupList = new List<string>();
                    webFormFieldGroupList.Clear();
                    string tablename;// = ((WebFormProperty)Util.PropertyGrid.SelectedObject).Table;
                    if (Util.SelectedNode.Parent != null)
                    {
                        if (Util.SelectedNode.Parent.Parent != null)
                        {
                            SysTreeNode webForm = (SysTreeNode)Util.SelectedNode.Parent.Parent;
                            tablename = ((WebFormProperty)webForm.Property).Table;
                            //如果设置了Table则检索Table下的Group，否则返回空列表
                            if (!string.IsNullOrEmpty(tablename))
                            {
                                XmlNodeList nodeList = Util.SysXmlDocument.SelectNodes(TablePath + tablename + "/FieldGroups/*");
                                foreach (XmlNode node in nodeList)
                                {
                                    webFormFieldGroupList.Add(node.Name);
                                }
                            }
                        }
                    }
                }
                return webFormFieldGroupList;
            }
        }

        public List<string> TableGroupFieldPropertyFieldsList
        {
            get
            {
                if (tableGroupField == null || tableGroupFieldExpire)
                {
                    tableGroupField = new List<string>();
                    tableGroupField.Clear();
                    XmlNodeList nodelist =
                        Util.SysXmlDocument.SelectNodes(TablePath +
                                                        Util.SelectedNode.SysXmlNode.ParentNode.ParentNode.ParentNode.
                                                            Name + "/Fields/*");
                    foreach (XmlNode node in nodelist)
                    {
                        tableGroupField.Add(node.Name);
                    }
                }
                return tableGroupField;
            }
        }

        public List<string> TableRelationField
        {
            get
            {
                if (tableRelationField == null || tableRelationFieldExpire)
                {
                    tableRelationField = new List<string>();
                    tableRelationField.Clear();
                    XmlNodeList nodelist =
                        Util.SysXmlDocument.SelectNodes(TablePath +
                                                        Util.SelectedNode.SysXmlNode.ParentNode.ParentNode.ParentNode.
                                                            Name + FieldsPath);
                    foreach (XmlNode node in nodelist)
                    {
                        tableRelationField.Add(node.Name);
                    }
                }
                return tableRelationField;
            }
        }

        public List<string> TableRelationRelatedField
        {
            get
            {
                if (tableRelationRelatedField == null || tableRelationRelatedFieldExpire)
                {
                    tableRelationRelatedField = new List<string>();
                    tableRelationRelatedField.Clear();
                    Util.SetPropertyInSilent = true;
                    Util.SetPropertyContent((SysTreeNode)Util.SelectedNode.Parent);
                    Util.SetPropertyInSilent = false;
                    string tableName = ((TableRelationProperty)((SysTreeNode)Util.SelectedNode.Parent).Property).Table;
                    XmlNodeList nodelist = Util.SysXmlDocument.SelectNodes(TablePath + tableName + FieldsPath);
                    foreach (XmlNode node in nodelist)
                    {
                        tableRelationRelatedField.Add(node.Name);
                    }
                }
                return tableRelationRelatedField;
            }
        }

        public List<string> QueryDataSourceTableFields
        {
            get
            {
                if (queryDataSourceTableFields == null || queryDataSourceTableFieldsExpire)
                {
                    queryDataSourceTableFields = new List<string>();
                    queryDataSourceTableFields.Clear();
                    string tableName;
                    if (Util.SelectedNode.Property is QueryDataSourceRangProperty)
                    {
                        tableName = ((QueryDataSourceRangProperty)Util.SelectedNode.Property).Table;
                    }
                    else
                    {
                        tableName = ((QueryDataSourceSortingFieldProperty)Util.SelectedNode.Property).Table;
                    }
                    XmlNodeList nodelist = Util.SysXmlDocument.SelectNodes(TablePath + tableName + FieldsPath);
                    foreach (XmlNode node in nodelist)
                    {
                        queryDataSourceTableFields.Add(node.Name);
                    }
                }
                return queryDataSourceTableFields;
            }
        }
        public List<string> EnumList
        {
            get
            {
                if (enumsList == null || enumsListExpire)
                {
                    enumsList = new List<string>();
                    enumsList.Clear();
                    XmlNodeList nodeList = Util.SysXmlDocument.SelectNodes("对象设计器/数据字典/枚举 /" + "*");
                    foreach (XmlNode node in nodeList)
                    {
                        enumsList.Add(node.Name);
                    }
                }
                return enumsList;
            }
        }
    }
}